package com.rongkang.interview.juc.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * 阻塞队列3.0版本
 * 使用新版的阻塞队列生产者和消费者
 *
 * 线程操作资源类
 * 判断,干活,通知
 * 防止虚假唤醒
 */
class MyResource {
    //加上volatile，为了保证数据的可见性,也就是当FLAG修改的时候,马上通知其他线程进行修改
    private volatile boolean FLAG = true;
    //使用原子包装类，不用number++
    private AtomicInteger atomicInteger = new AtomicInteger();
    BlockingQueue<String> blockingQueue = null;

    //这里采用依赖注入里面的,构造注入方法
    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        //查询传入的class是什么
        System.out.println(blockingQueue.getClass().getName());
    }

    //生产者
    public void myProd() throws Exception {
        String data = null;
        boolean retValue;
        //如果为true，就进行+1
        while (FLAG) {
            data = atomicInteger.incrementAndGet() + "";//number++
            retValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (retValue) {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列 " + data + "成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列 " + data + "失败");
            }
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"\t 大老板叫停，生产结束");
        }

    }

    //消费者
    public void myConsumer() throws Exception {
        String retValue;
        while (FLAG) {
            //两秒取出一个
            retValue = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (retValue != null && retValue != "") {
                System.out.println(Thread.currentThread().getName() + "\t 消费队列" + retValue + "成功");
            } else {
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 消费失败，队列中为空，退出");
                //退出消费队列
                return;
            }
        }
        System.out.println(Thread.currentThread().getName()+"\t 停止生产");
    }

    /**
     * @Description: 停止生产的判断
     */
    public void stop() {
        this.FLAG = false;
    }


}

public class ProdConsumerTraditionPlusDemo {
    public static void main(String[] args) throws InterruptedException {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<String>(10));
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"生产线程启动");
            System.out.println();
            System.out.println();
            try {
                myResource.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"Prod").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"消费线程启动");
            try {
                myResource.myConsumer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"Consumer").start();
        TimeUnit.SECONDS.sleep(5);
        System.out.println();
        System.out.println();
        System.out.println("main线程叫停");
        myResource.stop();

    }
}
